package com.xyh.controller;

import com.xyh.entity.YxcAmount;
import com.xyh.service.YxcAmountService;
import com.xyh.service.YxcCarService;
import com.xyh.service.YxcUserService;
import com.xyh.service.YxcUsersYhqService;
import com.xyh.utils.Util;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
public class YxcAmountController {

    @Resource
    private YxcAmountService yxcAmountService;

    @Resource
    private YxcCarService yxcCarService;

    @Resource
    private YxcUsersYhqService yxcUsersYhqService;

    @Resource
    private YxcUserService yxcUserService;

    @RequestMapping("/to_yxc_amount")
    public String toYxcAmount(HttpServletRequest request, Model model){
        int car_id = Integer.parseInt(request.getParameter("car_id"));
        String car_number = request.getParameter("car_number");
        String owner_telephone = request.getParameter("owner_telephone");
        int current_page = Integer.parseInt(request.getParameter("current_page"));
        int page_size = Integer.parseInt(request.getParameter("page_size"));
        String start_time = Util.getTimeNow();

        model.addAttribute("car_id",car_id);
        model.addAttribute("car_number",car_number);
        model.addAttribute("owner_telephone",owner_telephone);
        model.addAttribute("start_time",start_time);
        model.addAttribute("current_page",current_page);
        model.addAttribute("page_size",page_size);
        return "yxc_amount";
    }

    @RequestMapping("/add_start_amount")
    public String addStartAmount(HttpServletRequest request, RedirectAttributes attributes, YxcAmount yxcAmount){
        int current_page = Integer.parseInt(request.getParameter("current_page"));
        int page_size = Integer.parseInt(request.getParameter("page_size"));
        int car_id = Integer.parseInt(request.getParameter("car_id"));

        int flag = yxcAmountService.addStartAmount(yxcAmount);
        if (flag == 0) throw new RuntimeException("添加失败");

        int res = yxcCarService.updateIsInitById(car_id,1);
        if (res == 0) throw new RuntimeException("修改失败");

        attributes.addAttribute("login_telephone",yxcAmount.getOwner_telephone());
        attributes.addAttribute("situation",0);
        attributes.addAttribute("current_page",current_page);
        attributes.addAttribute("page_size",page_size);
        return "redirect:to_yxc_main";
    }

    @RequestMapping("/finish_amount_jf")
    public String finishAmountJf(HttpServletRequest request,RedirectAttributes attributes,YxcAmount yxcAmount){
        String owner_telephone = request.getParameter("owner_telephone");
        int total_time = Integer.parseInt(request.getParameter("total_time"));
        int car_id = Integer.parseInt(request.getParameter("car_id"));
        int vip_state = Integer.parseInt(request.getParameter("vip_state"));
        String login_telephone = request.getParameter("login_telephone");
        System.err.println("finishAmountJf:" + login_telephone);
        yxcAmount.setSjjf_telephone(login_telephone);
        yxcAmount.setSjjf_time(yxcAmount.getEnd_time());   //这里实际缴费时间和出场时间写成一样的，有待完善
        if (total_time == 0) yxcAmountService.updateEndAmountFree(yxcAmount);    //不用缴费
        else {  //要缴费
            int xiaofei = Util.getNeedPayNumber(total_time - yxcAmount.getUse_yhq(), vip_state);  //yxc_user xiaofei
            yxcAmount.setSjjf_pay(xiaofei);
            int yhq_bh = yxcAmount.getUse_yhq();   //使用的优惠券编号（0 没使用）
            if (yhq_bh != 0) {   //使用消费券
                yxcUsersYhqService.updateNumByPhoneAndBh(owner_telephone, yhq_bh);   //剩余的量 - 1
                yxcUsersYhqService.deleteByPhoneAndBhAndNum(owner_telephone, yhq_bh);  //删除剩余的量为 0 的记录
                yxcUserService.updateYhqByPhone(owner_telephone);   //用户表的优惠券数量 - 1
            }
            yxcUserService.updateXiaofeiByPhone(owner_telephone,xiaofei);  //调整用户表的积分和消费   积分:消费 = 1:1
            if (yhq_bh != 0)yxcAmount.setWhat_yhq("停车"+yhq_bh+"小时抵用券");
            yxcAmountService.updateEndAmount(yxcAmount);
        }
        yxcCarService.updateIsInitById(car_id,0);
        attributes.addAttribute("login_telephone",login_telephone);
        attributes.addAttribute("situation",0);
        attributes.addAttribute("current_page",1);
        attributes.addAttribute("page_size",1);
        return "redirect:to_yxc_main";
    }

    @RequestMapping("/deleteByAmountId")
    public String deleteByAmountId(HttpServletRequest request,RedirectAttributes attributes){
        int amount_id = Integer.parseInt(request.getParameter("amount_id"));
        String login_telephone = request.getParameter("login_telephone");

        int flag = yxcAmountService.deleteByAmountId(amount_id);
        if (flag == 0) throw new RuntimeException("删除失败");

        attributes.addAttribute("login_telephone",login_telephone);
        attributes.addAttribute("situation",3);
        attributes.addAttribute("current_page",1);
        attributes.addAttribute("page_size",3);
        return "redirect:to_yxc_main";
    }

}
